﻿Imports microsoft.Data.SQLite

Public Class 注册账号
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then MsgBox("内容不能为空！") : Exit Sub
        If TextBox2.Text <> TextBox3.Text Then MsgBox("两次输入的密码不一致") : Exit Sub
        Using connection As New sqliteconnection(_connString)
            Try
                connection.Open()
                ' 检查账号是否已存在 
                Dim checkSql = "SELECT COUNT(*) FROM userlist WHERE 账号 = @account and 是否注销 =@isfalse"
                Using checkCmd As New SqliteCommand(checkSql, connection)
                    checkCmd.Parameters.AddWithValue("@account", TextBox1.Text.Trim)
                    checkCmd.Parameters.AddWithValue("@isfalse", "false")
                    Dim count = Convert.ToInt32(checkCmd.ExecuteScalar)
                    If count > 0 Then
                        MsgBox("已有相同账号，请修改", vbOKOnly, "提示")
                        Exit Sub
                    End If
                End Using
                ' 插入新用户 
                Dim insertSql = "INSERT INTO userlist (创建人,创建时间,修改人,是否注销,账号, 密码) VALUES (@cuser,@ctime,@muser,@isfalse,@account, @password)"
                Using insertCmd As New SqliteCommand(insertSql, connection)
                    insertCmd.Parameters.AddWithValue("@cuser", username)
                    insertCmd.Parameters.AddWithValue("@ctime", Now)
                    insertCmd.Parameters.AddWithValue("@muser", username)
                    insertCmd.Parameters.AddWithValue("@isfalse", "false")
                    insertCmd.Parameters.AddWithValue("@account", TextBox1.Text.Trim)
                    insertCmd.Parameters.AddWithValue("@password", EncryptString(TextBox2.Text))
                    insertCmd.ExecuteNonQuery()
                    MsgBox("注册成功！", vbOKOnly, "提示")
                End Using

            Catch ex As Exception
                MessageBox.Show($"操作失败: {ex.Message}")
            End Try
        End Using
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then MsgBox("内容不能为空！") : Exit Sub
        If TextBox2.Text <> TextBox3.Text Then MsgBox("两次输入的密码不一致") : Exit Sub
        Using connection As New sqliteconnection(_connString)
            Try
                connection.Open()
                ' 检查账号是否已存在 
                Dim checkSql = "SELECT COUNT(*) FROM userlist WHERE 账号 = @account"
                Using checkCmd As New sqliteCOMMAND(checkSql, connection)
                    checkCmd.Parameters.AddWithValue("@account", TextBox1.Text.Trim)
                    Dim count = Convert.ToInt32(checkCmd.ExecuteScalar)
                    If count > 0 Then
                        Dim updateSql As String = "update userlist SET 是否注销 = @isconcel WHERE 账号 = @account"
                        Dim updateCmd As New sqliteCOMMAND(updateSql, connection)
                        updateCmd.Parameters.AddWithValue("@account", TextBox1.Text.Trim())
                        updateCmd.Parameters.AddWithValue("@isconcel", "false")
                        updateCmd.ExecuteNonQuery()
                        MsgBox("恢复账号成功！", vbOKOnly, "提示")
                    Else
                        MsgBox("账号或密码不正确！", vbOKOnly, "提示")
                    End If
                End Using

            Catch ex As Exception
                MessageBox.Show($"操作失败: {ex.Message}")
            End Try
        End Using
    End Sub

    Private Sub 注册账号_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GroupBox1.Left = Me.Left
        GroupBox1.Top = Me.Top
    End Sub
End Class